//
//  LDTHomePageViewController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "LDTHomePageViewController.h"
#import "CK_MenuViewController.h"
#import "LDTNetWorkRequestManager.h"
#import "LDTCollectionViewCell.h"
#import "LDTHomeModel.h"
#import "LDTCycleView.h"
#import "LDTCycleModel.h"
#import "LDTHomeDetailController.h"

#import "LDTTopicController.h"
#import "LDTProductController.h"
#import "AWRadioHomeController.h"



@interface LDTHomePageViewController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

///CollectionView
@property(nonatomic,strong)UICollectionView *collectionView;
///数据源数组
@property(nonatomic,strong)NSMutableArray *dataSource;
///轮播图属性
@property(nonatomic,strong)LDTCycleView *cycleView;
///存放轮播图的数组
@property(nonatomic,strong)NSMutableArray *cycleArray;


@end

@implementation LDTHomePageViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    [CK_MenuViewController getMenuViewController].menuArray = [@[@"阅读·READING",@"电台·RADIO",@"话题·TOPICS",@"良品·PRODUCTS",@"订阅·SUBSCRITION",@"设置·SETTING"]mutableCopy];
    
    self.title = @"阅读页面";
    self.dataSource = [NSMutableArray new];
    self.cycleArray = [NSMutableArray new];
    
    
    [self getViews];
    [self delegates];
    [self getData];
    
  
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"LDTCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    //注册头轮播图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

- (void)getViews{
    
    //创建CollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    //设置layout
    layout.itemSize = CGSizeMake((self.view.frame.size.width-3)/3, (self.view.frame.size.width-3)/3);
    layout.minimumInteritemSpacing = 1.5;
    layout.minimumLineSpacing = 1.5;
    //创建头轮播图
    self.cycleView = [[LDTCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 230) InterVal:3];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 230);
    
    [self.view addSubview:self.collectionView];
}

- (void)delegates
{
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    

}

- (void)getData
{
    [LDTNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] Method:LDT_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        if (data) {
            NSMutableDictionary *jsonDict =  [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
          //  NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
            NSArray *array = jsonDict[@"data"][@"list"];
            NSArray *arr  = jsonDict[@"data"][@"carousel"];
            
            for (NSDictionary *diccy in arr) {
                
                LDTCycleModel *model = [[LDTCycleModel alloc]init];
                [model setValuesForKeysWithDictionary:diccy];
                
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.img]];
                UIImage *image = [UIImage imageWithData:data];
                
                [self.cycleArray addObject:image];
            }
            
            
            for (NSDictionary *dic in array) {
                LDTHomeModel *model = [[LDTHomeModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataSource addObject:model];
            }
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            
            self.cycleView.imgNameArray = self.cycleArray;
            
         //   self.cycleView.imgNameArray = [@[@"1",@"2",@"3",@"4",@"5"]mutableCopy];
        
        });
        
    }];

}

#pragma mark - 点击UICollectionView跳转至详情页面

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LDTHomeDetailController *detaileVC = [[LDTHomeDetailController alloc]init];
    
    LDTHomeModel *model = self.dataSource[indexPath.row];
    detaileVC.type = [NSString stringWithFormat:@"%ld",model.type];
    
    [self.navigationController pushViewController:detaileVC animated:YES];
}




#pragma mark -UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LDTCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}


#pragma mark -UICollectionViewDelegate

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
       
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        [header addSubview:self.cycleView];
        return header;
    }
    return nil;
}



#pragma mark - 点击菜单项的代理

- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title
{
    [self.navigationController popToRootViewControllerAnimated:NO];
    switch (index) {
        case 0:
        {
                   }
            break;
        case 1:
        {
            AWRadioHomeController *radioHomeVC = [[AWRadioHomeController alloc]init];
            [self.navigationController pushViewController:radioHomeVC animated:YES];

        }
            break;
        case 2:
        {
            LDTTopicController *topicVC = [[LDTTopicController alloc]init];
            [self.navigationController pushViewController:topicVC animated:YES];
        }
            break;
        case 3:
        {
            LDTProductController *productVC = [[LDTProductController alloc]init];
            [self.navigationController pushViewController:productVC animated:YES];
    
        }
            break;

            
        default:
            break;
    }
}

//点击抽屉按钮
- (IBAction)didClickLeftBarBtnItem:(UIBarButtonItem *)sender
{
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController]hideLeftViewController];
    }else{
        [[CK_MenuViewController getMenuViewController]showLeftViewController];
    }
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
